#import <UIKit/UIKit.h>
#import <UIKit/UIControl.h>
#import <UIKit/UIFont.h>
#import <UIKit/UIColor.h>
// #import <UIKit/UIGeometry.h>	// UIEdgeInsetsMaek()

#import "NumericPanel.h"
#import "QtKey.h"

typedef enum
{
	UIButtonTypeCustom = 0,
	UIButtonTypeRoundedRect,
	UIButtonTypeDetailDisclosure,
	UIButtonTypeInfoLight,
	UIButtonTypeInfoDark,
	UIButtonTypeContactAdd,
} UIButtonType;

enum
{
	UIControlEventTouchDown           = 1 <<  0,
	UIControlEventTouchDownRepeat     = 1 <<  1,
	UIControlEventTouchDragInside     = 1 <<  2,
	UIControlEventTouchDragOutside    = 1 <<  3,
	UIControlEventTouchDragEnter      = 1 <<  4,
	UIControlEventTouchDragExit       = 1 <<  5,
	UIControlEventTouchUpInside       = 1 <<  6,
	UIControlEventTouchUpOutside      = 1 <<  7,
	UIControlEventTouchCancel         = 1 <<  8,
	
	UIControlEventValueChanged        = 1 << 12,
	
	UIControlEventEditingDidBegin     = 1 << 16,
	UIControlEventEditingChanged      = 1 << 17,
	UIControlEventEditingDidEnd       = 1 << 18,
	UIControlEventEditingDidEndOnExit = 1 << 19,
	
	UIControlEventAllTouchEvents      = 0x00000FFF,
	UIControlEventAllEditingEvents    = 0x000F0000,
	UIControlEventApplicationReserved = 0x0F000000,
	UIControlEventSystemReserved      = 0xF0000000,
	UIControlEventAllEvents           = 0xFFFFFFFF
};

enum
{
	UIControlStateNormal               = 0,
	UIControlStateHighlighted          = 1 << 0,
	UIControlStateDisabled             = 1 << 1,
	UIControlStateSelected             = 1 << 2,
	UIControlStateApplicationReserved  = 0x00FF0000,
	UIControlStateReserved             = 0xFF000000
};

@class UIButton;

@implementation NumericPanel

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
	if ((self = [super initWithFrame:frame]) == nil)	return self;
	
	delegate = nil;
	
	keySets = [[NSMutableDictionary alloc] initWithCapacity:8];
	
	[self setBackgroundColor:[UIColor blackColor]];
	
	[self addSubview:[self buttonWithFrame:CGRectMake(0, 0, 80, 80) title:@"BS" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Backspace]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(80, 0, 80, 80) title:@"/" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Slash]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(160, 0, 80, 80) title:@"*" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Asterisk]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(240, 0, 80, 80) title:@"-" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Minus]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(0, 80, 80, 80) title:@"7" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:'7']]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(80, 80, 80, 80) title:@"8" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:'8']]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(160, 80, 80, 80) title:@"9" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:'9']]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(240, 80, 80, 80) title:@"+" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Plus]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(0, 160, 80, 80) title:@"4" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:'4']]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(80, 160, 80, 80) title:@"5" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:'5']]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(160, 160, 80, 80) title:@"6" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:'6']]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(240, 160, 80, 80) title:@"=" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Equal]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(0, 240, 80, 80) title:@"1" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:'1']]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(80, 240, 80, 80) title:@"2" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:'2']]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(160, 240, 80, 80) title:@"3" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:'3']]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(240, 240, 80, 80) title:@"Tab" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Tab]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(0, 320, 80, 80) title:@"0" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:'0']]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(80, 320, 80, 80) title:@"," keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Comma]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(160, 320, 80, 80) title:@"." keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Period]]]]];
	[self addSubview:[self buttonWithFrame:CGRectMake(240, 320, 80, 80) title:@"Ent" keyArray:[NSArray arrayWithObject:[NSArray arrayWithObject:[NSNumber numberWithInt:Key_Return]]]]];
	
	return self;
}

- (void)dealloc
{
	[super dealloc];
}

- (UIButton *)buttonWithFrame:(CGRect)frame title:(NSString *)title keyArray:(NSArray *)keys
{
	UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];
	// [b setRotationBy:90.0];
	[b setFrame:frame];
	[b setShowsTouchWhenHighlighted:YES];
	[b setBackgroundColor:[UIColor blackColor]];
	[b setFont:[UIFont systemFontOfSize:36.0]];
	// [b setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
	[b setBackgroundImage:[UIImage imageNamed:@"Key.png"] forState:UIControlStateNormal];
	[b setTitle:title forState:UIControlStateNormal];
	[b addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
	[b addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpInside];
	[b addTarget:self action:@selector(buttonReleased:) forControlEvents:UIControlEventTouchUpOutside];
	
	[keySets setObject:keys forKey:[NSNumber numberWithInt:b]];
	
	return b;
}

- (void)buttonPressed:(UIButton *)button
{
	if ([[NSUserDefaults standardUserDefaults] boolForKey: @"enable_click_sound"])
		AudioServicesPlaySystemSound(1105);
	
	NSArray *keyCodes;
	if ((keyCodes = (NSArray *)[keySets objectForKey:[NSNumber numberWithInt:button]]) == nil)	return;
	
	for (int keys = 0; keys != [keyCodes count]; ++keys)
	{
		NSArray *sets = [keyCodes objectAtIndex:keys];
		for (int set = 0; set != [sets count]; ++set)
		{
			int keyCode = [[sets objectAtIndex:set] intValue];
			[delegate qtKeyPressed:keyCode];
		}
	}
}

- (void)buttonReleased:(UIButton *)button
{
	NSArray *keyCodes;
	if ((keyCodes = (NSArray *)[keySets objectForKey:[NSNumber numberWithInt:button]]) == nil)	return;
	
	for (int keys = 0; keys != [keyCodes count]; ++keys)
	{
		NSArray *sets = [keyCodes objectAtIndex:keys];
		for (int set = ([sets count] - 1); 0 <= set; --set)
		{
			int keyCode = [[sets objectAtIndex:set] intValue];
			[delegate qtKeyReleased:keyCode];
		}
	}
}

@end
